﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Newtonsoft.Json;
using Net5ApiANdWpfClient.Lib.Model.SystemAdmin;
using Net5ApiANdWpfClient.Lib.Common;
using Net5ApiANdWpfClient.Lib.Model;

namespace Net5ApiANdWpfClient.Lib.Service
{
    public class AdminUserService
    {
        private static string token = "";
        private static LoginUserView loginUser;
        public static string Token { get { return token; } }
        public static LoginUserView LoginUser {
            get { return loginUser; } 
        }
        public LoginUserView Login(string u, string p)
        {
           string apiUrl = $"api/AdminUserManager/Login"; //请求api的路径
           string postData="{\"username\":\""+u+"\",\"password\":\""+p+"\",\"clientType\":"+ PcConfig.ClientType + "}";
            ResultModel result = HttpRequestTools.Post(apiUrl, postData);
            if (result.code == ResultCode.Ok)  //返回结果正确
            {
                dynamic data = result.data;  //将数据返回给动态对象
                loginUser = JsonConvert.DeserializeObject<LoginUserView>(JsonConvert.SerializeObject(data));
                token = loginUser.token;
                return loginUser;
            }
            else
            {
                return null;
            }
        }


        public bool IsLogin()
        {
            if (string.IsNullOrEmpty(token)) return false;
            return true;
        }
        public void LoginOut()
        {
            token = null;
            loginUser = null;
        }
        /// <summary>
        /// 用户搜索方法
        /// </summary>
        /// <param name="loginName">登录名</param>
        /// <param name="fullName">全名</param>
        /// <param name="CurrentPageIndex">当前页</param>
        /// <returns></returns>
        public PagerResult<AdminUser> SearchPage(string card, string account, int CurrentPageIndex)
        {
            string apiUrl = "api/Operator/GetPageList"; //请求api的路径
            SearchModel search = new SearchModel()
            {
                SearchItem = new Dictionary<string, string> { { "op_card", card }, { "op_account", account } },
                page = CurrentPageIndex,
                limit = PcConfig.PageSize
            };
            ResultModel result = HttpRequestTools.PostByToken(apiUrl, JsonConvert.SerializeObject(search));
            if (result.code == ResultCode.Ok)
            {
                return JsonConvert.DeserializeObject<PagerResult<AdminUser>>(JsonConvert.SerializeObject(result.data));
            }
            else
            {
                return null;
            }

        }
      
    }
}
